function toggleMenu(){
    const sideBar = document.getElementById("NavigationSideBar");
    const btnText = document.querySelectorAll("span.BtnText");
    if (sideBar.hasAttribute('open')){
        Array.from(btnText).forEach(element => {
            element.style.opacity = '0';
            setTimeout(() => {
                element.style.display = 'none';
                sideBar.removeAttribute('open')
            }, 50)
        });
    } else {
        sideBar.toggleAttribute('open');
        setTimeout(() => {
            Array.from(btnText).forEach(element => {
                element.style.display = 'block';
                setTimeout(() => {
                    element.style.opacity = '1';
                }, 1)
            });
        }, 200);
    }
}

function closeMenu(){
    const sideBar = document.getElementById("NavigationSideBar");
    const btnText = document.querySelectorAll("span.BtnText");
    if (sideBar.hasAttribute('open')){
        Array.from(btnText).forEach(element => {
            element.style.opacity = '0';
            setTimeout(() => {
                element.style.display = 'none';
                sideBar.removeAttribute('open')
            }, 50)
        });
    }
}

function setTheme(type){
    const style = document.getElementById('theme');
    type = (type && type.toLowerCase()) || 'dark';
    styleDict = {'light':'assets/css/light.css', 'dark':'assets/css/dark.css'}
    if (type in styleDict){
        style.setAttribute('href', styleDict[type])
    }
    document.querySelector('#customStyle').innerHTML = `
        button:not(.NavigationBtn):hover, button.qfwButton:hover{
            ${type == 'dark' ? 'opacity: 0.9' : 'background-color: rgb(245, 245, 245);' }
        }
    `
}

function writeID(){
    id = document.getElementById('MinecraftID').value
    if (id != ''){ localStorage.setItem('ID', id) }
    checkIfHasID()
}

function callServer(route, args, num, func){
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    try {
                        const response = JSON.parse(xhr.responseText.replace(/'/g, '"'));
                        resolve([response, num]);
                    } catch {
                        resolve(['OK', num]);
                    }
                } else {
                    reject(xhr.status);
                }
            }
        };
        var addr = 'http://154.7.177.68:3930'
        // var addr = 'http://127.0.0.1:3930'
        if (func === 'GET'){
            xhr.open(func, `${addr}${route}?${args}`, true);
            xhr.send();
        } else {
            xhr.open(func, `${addr}${route}`, true)
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.send(args)
        }
    });
}

function callOpenPage(urlType){
    const urlDict = {
        0: 'https://gitee.com/JingHai-Lingyun/RIT-ToolKit/issues'
    }
    window.open(urlDict[urlType], '_blank')
}

setTheme(localStorage.getItem('theme'))
document.querySelector(`input#${localStorage.getItem('theme')}`).toggleAttribute('checked')